#!/usr/bin/ruby -w
require 'optparse'
require 'ftools'
CORE_FILES = %w[marco config.xml.sample ircbot.rb version_number README LICENSE]
CORE_PLUGS = %w[auth die do help plugin restart stfu]
FULL_PLUGS = %w[conversationalist dieroll lyrics macro michaelis urbandict whatis wiki wikilib calc isdown poll decision_making define]
DATA_FILES = %w[conversationalist facts macros]
options = {}
OptionParser.new do |opts|
	opts.banner = "Usage: #{$0} [options]"
	
	opts.on('-u','--username USERNAME',"Google code username") do |name|
		options['username'] = name
	end
	opts.on('-p','--password PASSWORD','Google code password') do |pass|
		options['password'] = pass
	end
	opts.on('--core','Copy only the core files.') do |core|
		options['core'] = core
	end
end.parse!

if options['username'] and options['password'] then
	puts "Creating a directory for the release..."
	if options['core'] then
		dir = "marcobot-core-#{File.read('version_number')}"
	else
		dir = "marcobot-full-#{File.read('version_number')}"
	end
	Dir.mkdir(dir)
	Dir.mkdir("#{dir}/plugins")
	Dir.mkdir("#{dir}/plugins/data")
	puts "Copying core files..."
	for file in CORE_FILES
		File.copy(file,"#{dir}")
	end
	puts "Copying core plugins..."
	for file in CORE_PLUGS
		File.copy("plugins/#{file}.rb","#{dir}/plugins")
	end
	unless options['core']
		puts "Copying other plugins..."
		for file in FULL_PLUGS
			File.copy("plugins/#{file}.rb","#{dir}/plugins")
		end
		puts "Starting data files..."
		for file in DATA_FILES
			system("touch #{dir}/plugins/data/#{file}.yml")
		end
	end
	puts "Done! Tarring up..."
	system("tar cvf #{dir}.tar #{dir}/")
	system("bzip2 #{dir}.tar")
	puts "Done! Cleaning up..."
	system("rm -rv #{dir}")
	puts "Will now upload." # TODO: write own implementation
	if options['core'] then
		summary = "MarcoBot version #{File.read('version_number')}, core files and plugins"
	else
		summary = "MarcoBot version #{File.read('version_number')}"
	end
	project = 'marcobot'
	system("python utils/upload.py -l \"Type-Archive,Opsys-All\" -s \"#{summary}\" -p #{project} -u #{options['username']} -w #{options['password']} #{dir}.tar.bz2")
	puts "Removing tarball..."
	File.delete("#{dir}.tar.bz2")
else
	puts "I need to know your username and password."
end
